#!/bin/bash

# 定义颜色代码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color

# 显示卸载标题
clear
echo -e "${RED}===============================================${NC}"
echo -e "${WHITE}           Syncthing 完全卸载脚本           ${NC}"
echo -e "${RED}===============================================${NC}"
echo -e "${WHITE}本脚本将执行以下操作：${NC}"
echo -e "${YELLOW}1. 停止并禁用Syncthing服务"
echo -e "2. 删除systemd服务文件"
echo -e "3. 移除安装目录(/opt/syncthing)"
echo -e "4. 清理防火墙规则（保留备份）"
echo -e "5. 提示用户手动清理配置文件${NC}"
echo -e "${RED}===============================================${NC}"

# 确认卸载
read -p "确定要完全卸载Syncthing吗？此操作不可逆！ [y/N] " confirm
if [[ ! $confirm =~ ^[Yy]$ ]]; then
    echo -e "${GREEN}已取消卸载${NC}"
    exit 0
fi

# 1. 停止并禁用服务
echo -e "${BLUE}[1/5]${NC} ${WHITE}正在停止服务...${NC}"
sudo systemctl stop syncthing 2>/dev/null
sudo systemctl disable syncthing 2>/dev/null

# 2. 删除服务文件
echo -e "${BLUE}[2/5]${NC} ${WHITE}删除服务配置...${NC}"
sudo rm -f /etc/systemd/system/syncthing.service
sudo systemctl daemon-reload
sudo systemctl reset-failed

# 3. 删除安装文件
echo -e "${BLUE}[3/5]${NC} ${WHITE}清理安装文件...${NC}"
sudo rm -rf /opt/syncthing

# 4. 清理防火墙规则（保留备份）
echo -e "${BLUE}[4/5]${NC} ${WHITE}移除防火墙规则...${NC}"

# 检查并清理firewalld规则（不删除备份）
if systemctl is-active --quiet firewalld; then
    echo -e "${YELLOW}保留现有的firewalld备份文件："
    ls -l /etc/firewalld/firewalld_backup_* 2>/dev/null || echo "未找到备份文件"
    sudo firewall-cmd --permanent --remove-port=8384/tcp 2>/dev/null
    sudo firewall-cmd --permanent --remove-port=22000/tcp 2>/dev/null
    sudo firewall-cmd --permanent --remove-port=21027/udp 2>/dev/null
    sudo firewall-cmd --reload
    echo -e "${GREEN}Firewalld规则已清理（备份保留）${NC}"
# 检查并清理iptables规则（不删除备份）
elif command -v iptables &> /dev/null; then
    echo -e "${YELLOW}保留现有的iptables备份文件："
    ls -l /etc/sysconfig/iptables_backup_* 2>/dev/null || echo "未找到备份文件"
    sudo iptables -D INPUT -p tcp --dport 8384 -j ACCEPT 2>/dev/null
    sudo iptables -D INPUT -p tcp --dport 22000 -j ACCEPT 2>/dev/null
    sudo iptables -D INPUT -p udp --dport 21027 -j ACCEPT 2>/dev/null
    sudo service iptables save 2>/dev/null
    echo -e "${GREEN}iptables规则已清理（备份保留）${NC}"
else
    echo -e "${YELLOW}未检测到防火墙服务，跳过规则清理${NC}"
fi

# 5. 显示后续操作提示
echo -e "${BLUE}[5/5]${NC} ${WHITE}卸载完成！${NC}"
echo -e "${RED}===============================================${NC}"
echo -e "${WHITE}以下文件需要手动清理：${NC}"
echo -e "${YELLOW}1. 配置文件目录: /root/.config/syncthing/"
echo -e "2. 默认同步文件夹: /root/Sync/"
echo -e "3. 可能存在的日志文件: /var/log/syncthing.log${NC}"
echo -e ""
echo -e "${WHITE}如需完全清理，请执行：${NC}"
echo -e "sudo rm -rf /root/.config/syncthing/"
echo -e "${RED}===============================================${NC}"

# 最终验证
if [ ! -d "/opt/syncthing" ] && [ ! -f "/etc/systemd/system/syncthing.service" ]; then
    echo -e "${GREEN}验证：Syncthing 已完全卸载${NC}"
else
    echo -e "${RED}警告：部分残留文件可能需要手动清理${NC}"
    echo -e "${YELLOW}残留文件检查："
    ls -ld /opt/syncthing /etc/systemd/system/syncthing.service 2>/dev/null
fi